<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="工序名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入工序名"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注"></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24" v-for="(item, index) in model.steps" :key="index">
            <a-form-model-item
              :label="'工序步骤' + (index + 1)"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="steps"
            >
              <a-input-search v-model="item.name" placeholder="请输入工序步骤" @search="removeStep(index)">
                <a-button slot="enterButton" icon="minus">移除步骤</a-button>
              </a-input-search>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-button type="primary" @click="addStep">添加步骤</a-button>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction } from '@/api/manage'

export default {
  name: 'HfStepForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        name: [{ required: true, message: '请输入工序名!' }]
      },
      url: {
        add: '/production/hfStep/add',
        edit: '/production/hfStep/edit',
        queryById: '/production/hfStep/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.modelDefault.steps = [
        {
          name: ''
        }
      ]
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      let httpurl = ''
      let method = ''
      if (!this.model.id) {
        httpurl += this.url.add
        method = 'post'
      } else {
        httpurl += this.url.edit
        method = 'put'
      }
      let model = Object.assign({}, this.model)
      if (this.model.steps[0].name === '') {
        this.$message.warn('步骤不能为空')
        return
      }

      model.steps = this.model.steps

      httpAction(httpurl, model, method)
        .then(res => {
          if (res.success) {
            that.$message.success(res.message)
            that.$emit('ok')
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          that.confirmLoading = false
        })
    },
    removeStep(index) {
      if (this.model.steps.length <= 1) {
        this.$message.warn('必须要有一个步骤')
        return
      }
      this.model.steps.splice(index, 1)
    },
    addStep() {
      this.model.steps.push({
        name: ''
      })
    }
  }
}
</script>
